const fs = require('fs');
class APP {
    constructor() {
        if (APP.instance) {
            return APP.instance;
        }
        APP.instance = this;
    }
    init(){

    }
    async saveScreenshot_error(androidBot,name){
        console.log('保存截图',name);
        let path = `${process.cwd()}/error/${name}_${Date.now()}.png`;
        let img = await androidBot.takeScreenshot();
        fs.writeFileSync(path,img);
    }
    findClass_v2(data, className,mathText){
        let classIndex = data.findIndex(item => {
            if(mathText){
                return item.className === className && item.text.includes(mathText);
            }
            return item.className === className;
        });
        if(classIndex == -1){
            return false;
        }
        return data[classIndex];
    }
    findClass_index(data, className) {
        // 查找元素
        let classIndex = data.findIndex(item => item.className === className);
        if(classIndex == -1){
            return false;
        }
        return classIndex + 1;
    }
    findClass_list(data, className){
        // 返回所有符合条件的数组
        let classIndex = data.filter(item => item.className === className);
        if(classIndex.length == 0){
            return false;
        }
        return classIndex;
    }
    getClassData(data, className){
        let classIndex = data.findIndex(item => item.className === className);
        if(classIndex == -1){
            console.error('错误的调用');
            return false;
        }
        return data[classIndex];
    }
    // 查找元素个数
    findClassCount(data, className){
        let classIndex = data.filter(item => item.className === className);
        return classIndex.length;
    }
    // 毫秒为单位
    sleep(time = 300){
        return new Promise(resolve => setTimeout(resolve, time));
    }
    async click(androidBot,x,y){
        let jude = await androidBot.click(x,y);
        if(jude === false){
            console.error('点击失败'); 
            return false;
        }
        return true;
    }
    // 从上往下滑动
    async swipeRect_t_b(androidBot,rect,time = 300){
        console.log('滑动矩形',rect);
        let startX = rect.left + (rect.right - rect.left) / 2; // 矩形中心x坐标 
        let startY = rect.top + (rect.bottom - rect.top) / 2; // 矩形中心y坐标
        let endX = startX; // 保持x不变
        let endY = rect.bottom; // 底部y坐标
        let jude = await androidBot.swipe(startX, startY, endX, endY, time); // 300ms的滑动时间
        if(jude === false){
            console.error('滑动失败'); 
            return false;
        }
        return true;
    }

     ocrDataFindText(data,text){
        let ocrText = data.find(item => item.text.includes(text));
        if(!ocrText){
            return false;
        }
        return true;
    }
    // text 是一个数组，我需要判断data数组中 肯定包含text数组中的每一项
    ocrDataFindTextArray(data,text){
        // 相当于遍历text 然后每个都执行一次ocrDataFindText
        let jude = text.every(item => this.ocrDataFindText(data,item));
        if(!jude){
            return false;
        }
        return true;
    }
    // 在一个区域内寻找文字 数据为数组格式data[x1,y1,x2,y2]
    async ocrRegionFindText(androidBot,data,text){
        let info = await this.ocrRegion(androidBot,data);
        return this.ocrDataFindText(info,text);
    }
     // 数据为数组格式data[x1,y1,x2,y2]
    async ocrRegion(androidBot,data){
        if(!data){
            let windowSize = await androidBot.getWindowSize();
            if (windowSize.width < windowSize.height) {
                data = [0, 0, windowSize.width, windowSize.height];
            } else {
                data = [0, 0, windowSize.height, windowSize.width];
            }
        }
      


        let result = await androidBot.ocr(data[0],data[1],data[2],data[3],0,0,0,1.0);
        if(!result){
            return null;
        }
        let info = [];
        for(let i = 0; i < result.length; i++){
            let item = result[i];
            info.push({
                text:item[1][0].toLowerCase(),
                // 相似度
                confidence:item[1][1],
                region:[item[0][0],item[0][1],item[0][2],item[0][3]]
            })
        }
        return info;
    }

    // 数据为数组格式data[x1,y1,x2,y2]
    async clickRegion(androidBot,data){
        let x = data[0] + (data[2] - data[0]) / 2;
        let y = data[1] + (data[3] - data[1]) / 2;
        let width = (data[2] - data[0]) / 4;
        let height = (data[3] - data[1]) / 4;
        let randX = x + Math.random() * width - width/2;
        let randY = y + Math.random() * height - height/2;
        let jude = await androidBot.click(randX,randY);
        if(jude === false){
            console.error('点击失败'); 
            return false;
        }
        return true;
    }
    // 点击矩形以中心为基准，点击随机位置
    async clickRect(androidBot,rect){
        if(!rect || isNaN(rect.left)){
            console.error('矩形不存在',rect);
            return false;
        }
        let x = rect.left + (rect.right - rect.left) / 2;
        let y = rect.top + (rect.bottom - rect.top) / 2;
        let width = (rect.right - rect.left) / 4;
        let x1 = x + Math.random() * width - width/2;
        let height = (rect.bottom - rect.top) / 4;
        let y1 = y + Math.random() * height - height/2;
        let jude = await androidBot.click(x1,y1);
        if(jude === false){
            console.error('点击失败'); 
            return false;
        }
        return true;
    }
}

// 创建单例实例
const instance = new APP();
Object.freeze(instance);

module.exports = instance;
